
package cn.afactory.tygz.ui.base.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.utils.ImageUtils;
import cn.afactory.tygz.ui.base.utils.sns.ViewListener;

public class SubjectImgGirdAdapter extends BaseAdapter {

    private Context mContext;
    LayoutInflater inflater = null;

    List<String> mPath = new ArrayList<String>();
    int mHeight = 0, mWidth = 0;
    public ViewListener mListener;

    public SubjectImgGirdAdapter(Context context,
                                 List<String> pics, int Width, int Height) {
        mContext = context;
        mPath = pics;
        inflater = LayoutInflater.from(context);
        mWidth = Width;
        mHeight = Height;
    }

    @Override
    public int getCount() {
        if (mPath != null) {
            return mPath.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mPath != null && mPath.size() > 0) {
            return mPath.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        String item = null;
        if (mPath != null && getCount() > 0) {
            item = (String) getItem(position);
        }

        View view = convertView;
        ViewHolder mHolder = null;
        if (view == null || !(view.getTag() instanceof ViewHolder)) {
            view = inflater.inflate(R.layout.list_item_forum_grid, parent, false);
            mHolder = new ViewHolder();
            mHolder.list_item_img = (ImageView) view.findViewById(R.id.forum_iv);
            view.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) view.getTag();
        }

        if (item == null) {
            return null;
        }

        try {
            if (!TextUtils.isEmpty(item)) {
                mHolder.list_item_img.setScaleType(ImageView.ScaleType.CENTER_CROP);
                String str = item.toLowerCase();
                int nwidth = mWidth > 0 ? mWidth : mContext.getResources().getDimensionPixelOffset(R.dimen.tv_scan_margin_top);
                BaseImgView.loadimg(mHolder.list_item_img, ImageUtils.getThumbnailFullPath(item,
                                nwidth + "x" +
                                        nwidth), R.drawable.ic_pic_loading_default_small,
                        R.drawable.ic_pic_loading_default_small, R.drawable.ic_pic_loading_default_small, ImageScaleType.EXACTLY,
                        nwidth,
                        nwidth, 0);
                if (mListener != null) {
                    mHolder.list_item_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListener.onClick(position, mPath);
                        }
                    });
                }
            } else {
                mHolder.list_item_img.setImageResource(R.drawable.ic_pic_loading_default_small);
            }
            int nheight = mHeight > 0 ? mHeight : (int) mContext.getResources().getDimension(R.dimen.tv_scan_margin_top)
                    + (int) mContext.getResources().getDimension(R.dimen.tv_scan_padding);

            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = nheight;
            view.setLayoutParams(layoutParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    public final class ViewHolder {
        ImageView list_item_img;
    }

    public List<String> getData() {
        return mPath;
    }

    public void setListener(ViewListener listener) {
        mListener = listener;
    }
}
